package com.kgc.service.impl;

import com.kgc.entity.Emp;
import com.kgc.entity.EmpExample;
import com.kgc.mapper.EmpMapper;
import com.kgc.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @Author: luo
 * @date: 2024/4/5 18:26
 * @Description:
 * @Version: 1.0
 */
@Service("empService")
@Slf4j
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public Emp findById(Integer id) {
        return empMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Emp> findAll() {
        return empMapper.selectByExample(null);
    }

    @Override
    public int addEmp(Emp emp) {
        System.out.println(emp);
        return empMapper.insert(emp);
    }

    @Override
    public int updateEmp(Emp emp) {
        return empMapper.updateByPrimaryKeySelective(emp);
    }

    @Override
    public int deleteEmp(Integer id) {
        return empMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteEmp(Integer[] ids) {
        EmpExample ex = new EmpExample();
        ex.createCriteria().andEmpnoIn(Arrays.asList(ids));
        return empMapper.deleteByExample(ex);
    }

    @Override
    public List<Emp> findPage(Integer pageNum, Integer pageSize) {
        pageNum = (pageNum -1) * pageSize;
        // 查询第pageNum页，每页pageSize条数据
        return empMapper.selectPage(pageNum, pageSize);
    }

    @Override
    public int upload(String name, MultipartFile[] files) {
            try {
                for (MultipartFile file:files){
                    log.info("name:{}"+name);
                    log.info("file:{}"+file);
                    String fileName = file.getOriginalFilename();
                    //获得文件后缀名
                    String suffix = fileName.substring(fileName.lastIndexOf("."));
                    //生成UUID
                    String uuid = UUID.randomUUID().toString();
                    //保存文件
                    file.transferTo(new File("L:/A/file/fileTest/"+uuid+suffix));

                }
                return 1;
            } catch (IOException e) {
                e.printStackTrace();
                return 0;
            }
    }

    @Override
    public int findCount() {
        return empMapper.findCount();
    }
}
